#include "wuShapeMovie.h"

wuShapeMovie::wuShapeMovie()
{

}

wuShapeMovie::~wuShapeMovie()
{
}

void wuShapeMovie::setup(string _filename, int _vw, int _vh)
{
 	movie.loadMovie(_filename);
	movie.play();
	movie.stop();

	movie.setLoopState(OF_LOOP_NONE);
//	movie.idleMovie();
	posSkin.x = 0;
	posSkin.y = 0;
	vw = _vw;
	vh = _vh;
	bIsAlive = false;
    scrollOffset = 0;
}


void wuShapeMovie::update()
{
	movie.idleMovie();
	skin.crop(movie.getPixels(),movie.getWidth(), movie.getHeight(), grayImg->getPixels(), vw, vh, posSkin.x, posSkin.y);

	if(!bLoop)
	{
		int f = movie.getCurrentFrame();
		int t = movie.getTotalNumFrames();
		if (f >= t) {
			movie.stop();
			movie.setFrame(1);
			bIsAlive=false;
		}//else
//		    movie.idleMovie();
	}
}

void wuShapeMovie::setMask(ofxCvGrayscaleImage * _grayImg)
{
//    grayImg.setFromPixels(_grayImg->getPixels(), vw, vh);
    grayImg=_grayImg;
}

void wuShapeMovie::draw(bool bDrawLines, int offX)
{
		// draw skin
		ofPushMatrix();
			ofEnableAlphaBlending();
//			movie.draw(posSkin.x+offX, posSkin.y);
			skin.draw(posSkin.x+scrollOffset+offX, posSkin.y);
			ofDisableAlphaBlending();
		ofPopMatrix();
}

void wuShapeMovie::setPosition(ofPoint _pos)
{
	posSkin =_pos;
	posSkin.x = posSkin.x + 5;
	posSkin.y = posSkin.y + 5;

}

ofPoint wuShapeMovie::getPosition()
{
	return posSkin;
}

/*


void wuShapeMovie::dragOff()
{
	bDragOn = false;
}

void wuShapeMovie::drag(int _x,int _y)
{
	if(!bDragOn)
	{
		offset.x = _x - posSkin.x;
		offset.y = _y - posSkin.y;
		bDragOn = true;
	}

	posSkin.x = _x -offset.x;
	posSkin.y = _y -offset.y;
}

*/
void wuShapeMovie::play()
{
    bIsAlive = true;
	movie.setFrame(1);
	movie.play();
}

void wuShapeMovie::clear()
{
	movie.stop();
	movie.setFrame(1);
	posSkin.x = 0;
	posSkin.y = 0;
	bIsAlive = false;
}

void wuShapeMovie::setLoopState(bool _loop)
{
    bLoop = _loop;
    if (_loop)
        movie.setLoopState(OF_LOOP_NORMAL);
    else
        movie.setLoopState(OF_LOOP_NONE);
}

bool wuShapeMovie::isAlive()
{
    return bIsAlive;
}

void wuShapeMovie::setScrollOffset(int _scrollOffset)
{
    // defineixo l' offset en el moment de crear-lo
    scrollOffset = _scrollOffset;
}

